<template>
  <div class="bigbox">
    <div class="top">
      <el-form
        ref="form"
        :model="form"
        label-width="40px"
        label-position="left"
      >
        <el-row :gutter="20">
          <el-col :span="5">
            <el-form-item label="用户">
              <FilterSelect2
                v-model="form.orgNo"
                :optionlist="selectList"
                :valuekey="{ name: 'name', value: 'companyNo' }"
              ></FilterSelect2>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="日期">
              <el-date-picker
                size="small"
                v-model="form.formTime"
                type="daterange"
                style="width: 100%"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="13" style="text-align: right">
            <el-form-item>
              <Twobtn @chaxun="getDyList" @chongzhi="reset"></Twobtn>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="tabs">
      <el-tabs v-model="activeName" @tab-click="tabsClick">
        <el-tab-pane
          v-for="item in dyList"
          :key="item.id"
          :label="item.name"
          :name="item.id"
          style="padding: 0 40px"
        ></el-tab-pane>
      </el-tabs>
    </div>
    <div class="main">
      <TopTile @command="command" @daochu="daochu" @shuaxin="init"></TopTile>
      <div class="line">
        <div class="left" v-loading="loading2">
          <div
            class="item"
            style="background-color: #ffa6a6; margin-bottom: 20px"
          >
            <div class="icon"></div>
            <div class="content">
              <div class="num">{{ entity.imbalanceTimes }}</div>
              <div class="label">不平衡次数</div>
            </div>
          </div>
          <div class="item" style="background-color: #98beff">
            <div class="icon"></div>
            <div class="content">
              <div class="num">{{ entity.currentImbalance }}%</div>
              <div class="label">当前不平衡度</div>
            </div>
          </div>
        </div>
        <div class="right">
          <nt-echart :loading="loading" :options="lineOptions"></nt-echart>
        </div>
      </div>
      <div class="table">
        <nt-table
          v-loading="loading2"
          height="90%"
          :issize="issize"
          :tableData="List"
          :tableConfig="tableConfig"
        >
        </nt-table>
      </div>
    </div>
  </div>
</template>

<script>
import { getDeviceChildrenApi, getDeviceRootApi } from '@/api'
import { getDyListApi } from '@/api/Intelligence-maintenance/Power-quality'
import TopTile from '@/components/TopTitle/index.vue'
import dayjs from 'dayjs'
import ntTable from '@/components/nt-table'
import { exportexcel } from '@/utils/excel'
import NtEchart from '@/components/nt-echart.vue'
import {
  getLineApi,
  getListApi
} from '@/api/Intelligence-maintenance/Power-quality/Current-imbalance'
export default {
  name: 'CurrentImbalance',
  components: {
    TopTile,
    NtEchart,
    ntTable
  },
  data() {
    return {
      form: {
        orgNo: '',
        formTime: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
      },
      selectList: [],
      activeName: '',
      dyList: [],
      Line: [],
      List: [],
      tableConfig: [
        {
          type: 'index',
          label: '序号',
          width: '100px',
          align: 'center'
        },
        {
          label: '时间',
          prop: 'time',
          width: '200px',
          align: 'center'
        },
        {
          label: 'Ia(V)',
          prop: 'ia',
          align: 'right'
        },
        {
          label: 'Ib(V)',
          prop: 'ib',
          align: 'right'
        },
        {
          label: 'Ic(V)',
          prop: 'ic',
          align: 'right'
        },
        {
          label: 'Iavg(V)',
          prop: 'iavg',
          align: 'right'
        },
        {
          label: '不平衡度(%)',
          prop: 'imbalance',
          align: 'right'
        }
      ],
      entity: {},
      issize: 'medium',
      loading: false,
      loading2: false
    }
  },
  created() {
    this.getTreeList()
  },
  computed: {
    lineOptions() {
      return {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        grid: {
          top: 50,
          bottom: 30,
          left: 100,
          right: 30
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          axisTick: {
            alignWithLabel: true
          }
        },
        yAxis: {
          type: 'value',
          name: '不平衡度(%)'
        },
        series: [
          {
            type: 'line',
            showSymbol: false,
            smooth: true,
            markPoint: {
              data: [
                { type: 'max', name: 'Max' },
                { type: 'min', name: 'Min' }
              ]
            },
            areaStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: '#5a96ff' // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: '#fff' // 100% 处的颜色
                  }
                ]
              }
            },
            data: this.Line.map((i) => {
              return [i.dateStr, i.value]
            })
          }
        ]
      }
    }
  },
  watch: {
    activeName: {
      handler(newVal) {
        Promise.all([this.getLine(newVal), this.getList(newVal)])
      }
    }
  },
  methods: {
    async getList(activeName) {
      this.loading2 = true
      if (activeName) {
        const res = await getListApi({
          deviceId: activeName,
          startTime: dayjs(this.form.formTime[0]).format('YYYY-MM-DD'),
          endTime: dayjs(this.form.formTime[1]).format('YYYY-MM-DD')
        })
        this.List = res.data.list
        this.entity = res.data.entity
      } else {
        this.List = []
        this.entity = {}
      }
      this.loading2 = false
    },
    async getLine(activeName) {
      this.loading = true
      if (activeName) {
        const res = await getLineApi({
          deviceId: activeName,
          startTime: dayjs(this.form.formTime[0]).format('YYYY-MM-DD'),
          endTime: dayjs(this.form.formTime[1]).format('YYYY-MM-DD')
        })
        this.Line = res.data.list
      } else {
        this.Line = []
      }
      this.loading = false
    },
    tabsClick(tab, event) {},
    init() {
      Promise.all([
        this.getLine(this.activeName),
        this.getList(this.activeName)
      ])
    },
    reset() {
      this.form.orgNo = this.selectList[0].companyNo
      this.form.formTime = [
        dayjs().format('YYYY-MM-DD'),
        dayjs().format('YYYY-MM-DD')
      ]
    },
    async getTreeList() {
      const res = await getDeviceRootApi()
      const res2 = await getDeviceChildrenApi(res.data.list[0])
      this.selectList = res2.data.list
      this.form.orgNo = this.selectList[0].companyNo
      this.getDyList()
    },
    async getDyList() {
      const res = await getDyListApi({
        orgNo: this.form.orgNo
      })
      this.dyList = res.data.list
      if (this.dyList.length > 0) {
        this.activeName = this.dyList[0].id
      } else {
        this.activeName = ''
      }
      Promise.all([
        this.getLine(this.activeName),
        this.getList(this.activeName)
      ])
    },
    command(val) {
      if (val === '默认') {
        this.issize = 'medium'
      } else if (val === '中等') {
        this.issize = 'small'
      } else {
        this.issize = 'mini'
      }
    },
    daochu() {
      exportexcel(
        '/pio-microservice-biz/pio/icurrent/imbalance/excel',
        {
          deviceId: this.activeName,
          startTime: dayjs(this.form.formTime[0]).format('YYYY-MM-DD'),
          endTime: dayjs(this.form.formTime[1]).format('YYYY-MM-DD')
        },
        '电流不平衡'
      )
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-tabs__nav-scroll {
  padding-left: 50px;
  border-top: 2px solid #e4e7ed;
}
.bigbox {
  display: flex;
  flex-direction: column;
  background-color: #f1f1f1;
  padding: 10px;
  height: 100%;
  .top {
    background-color: #fff;
    padding: 20px;
    box-sizing: border-box;
    height: 80px !important;
    width: 100% !important;
  }
  .tabs {
    background-color: #fff;
    padding: 0px 20px 0px 20px;
  }
  .main {
    flex: 1;
    display: flex;
    flex-direction: column;
    background-color: #fff;
    padding: 20px;
    .line {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 30px;
      height: 350px;
      .left {
        width: 15%;
        margin-right: 20px;
        .item {
          display: flex;
          height: 90px;
          padding: 20px;
          border-radius: 5px;
          .icon {
            width: 50px;
            height: 50px;
            background-color: #fff;
          }
          .content {
            flex: 1;
            text-align: center;
            color: #fff;
          }
        }
      }
      .right {
        flex: 1;
        height: 100%;
      }
    }
    .table {
      height: 50%;
    }
  }
}
</style>
